//
//  HCalendarView.swift
//  Drop
//
//  Created by hushaohua on 2018/5/7.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

private let stWeekStrings = ["一", "二", "三", "四", "五", "六", "日"];
private let stCalendarWeekdays = [2, 3, 4, 5, 6, 7, 1];

class HCalendarWeeksView : UIView{
    func weekButton(_ index:Int, title:String) -> UIButton {
        let width:CGFloat = (self.bounds.size.width - 10 - 6) / 7;
        let x:CGFloat = 5 + CGFloat(index) * (width + 1);
        let button = UIButton(frame: CGRect(x:x,y:0,width:width,height:self.bounds.height));
        button.setTitle(title, for: .normal);
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .light);
        button.backgroundColor = UIColor.white;
        button.setTitleColor(UIColor.gray, for: .normal);
        button.setTitleColor(UIColor.lightGray, for: .selected);
        return button;
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame);
        for index in 0..<7 {
            let button:UIButton = self.weekButton(index, title: stWeekStrings[index]);
            button.tag = stCalendarWeekdays[index];
            button.isSelected = index >= 6;
            self.addSubview(button);
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class HCalendarNavigationView : UIView{
    init(frame: CGRect, year:Bool) {
        super.init(frame: frame);
        self.addSubview(self.previousButton);
        self.addSubview(self.nextButton);
        if (!year){
            self.addSubview(self.monthButton);
        }else{
            self.yearButton.setTitleColor(UIColor.black, for: .normal);
        }
        self.addSubview(self.yearButton);
        self.addSubviewConstraints(year);
    }
    
    lazy var previousButton:UIButton = {
        var button:UIButton = UIButton(frame: .zero);
        button.translatesAutoresizingMaskIntoConstraints = false;
        button.setImage(UIImage(named: "backward"), for: .normal);
//        button.contentHorizontalAlignment = .left;
        return button;
    }();
    lazy var nextButton:UIButton = {
        var button:UIButton = UIButton(frame: .zero);
        button.translatesAutoresizingMaskIntoConstraints = false;
        button.setImage(UIImage(named: "forward"), for: .normal);
        button.contentMode = UIViewContentMode.scaleAspectFit;
//        button.contentHorizontalAlignment = .right;
        return button;
    }();
    
    lazy var monthButton:UIButton = {
        var button:UIButton = UIButton(frame: .zero);
        button.translatesAutoresizingMaskIntoConstraints = false;
        button.setTitleColor(UIColor.black, for: .normal);
        button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .medium);
        return button;
    }();
    lazy var yearButton:UIButton = {
        var button:UIButton = UIButton(frame: .zero);
        button.translatesAutoresizingMaskIntoConstraints = false;
        button.setTitleColor(UIColor.gray, for: .normal);
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium);
        return button;
    }();
    
    func addSubviewConstraints(_ year:Bool){
        var views:[String : UIView] = ["previous" : self.previousButton, "next" : self.nextButton, "year" : self.yearButton];
        var constraints:[NSLayoutConstraint] = [NSLayoutConstraint]();
        if (!year){
            views["month"] = self.monthButton;
            constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:[previous]-5-[month]-5-[next]", options: .directionLeftToRight, metrics: nil, views: views);
            constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-3-[month(==22)]-0-[year(==16)]", options: [.alignAllLeft, .alignAllRight], metrics: nil, views: views);
        }else{
            constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:[previous]-5-[year]-5-[next]", options: .directionLeftToRight, metrics: nil, views: views);
            constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[year]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: views);
        }
        
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-5-[previous(==80)]", options: .directionLeadingToTrailing, metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-5-[next(==80)]", options: .directionRightToLeft, metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[previous]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[next]-0-|", options: .directionLeadingToTrailing, metrics: nil, views: views);
        
        
        NSLayoutConstraint.activate(constraints);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class HCalendarPickedView : UIView{
    override init(frame: CGRect) {
        super.init(frame: frame);
        self.addSubview(self.fromButton);
        self.addSubview(self.toButton);
        self.addSubviewConstraints();
    }
    
    lazy var fromButton:UIButton = {
        var button:UIButton = UIButton(frame: .zero);
        button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
        button.translatesAutoresizingMaskIntoConstraints = false;
        button.setTitle("from", for: .normal);
        button.setTitleColor(UIColor.gray, for: .normal);
        button.contentHorizontalAlignment = .right;
        return button;
    }();
    lazy var toButton:UIButton = {
        var button:UIButton = UIButton(frame: .zero);
        button.contentEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);
        button.translatesAutoresizingMaskIntoConstraints = false;
        button.setTitle("to", for: .normal);
        button.setTitleColor(UIColor.gray, for: .normal);
        button.contentHorizontalAlignment = .left;
        return button;
    }();
    func addSubviewConstraints(){
        let views:[String : UIView] = ["from" : self.fromButton, "to" : self.toButton];
        var constraints:[NSLayoutConstraint] = [NSLayoutConstraint]();
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[from(==to)]-0-[to]-0-|", options: .alignAllCenterY, metrics: nil, views: views);
        constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[from(==to)]-0-|", options: .alignAllCenterY, metrics: nil, views: views);
        NSLayoutConstraint.activate(constraints);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
